var loadFCK;

Form = new Class({
    options: {},
    initialize: function (WS, FC, BC, args,  containerId){
        var container = $(containerId);
        options = args;

        var YUI = YAHOO.util;
        form = new YAHOO.inputEx.Form({
                fields:FC
                ,parentEl: containerId 
                ,buttons: BC
                ,onSubmit: function(e) {
                    YUI.Event.stopEvent(e); 

                    if (!this.validate()){
                        return;
                    }
                    data = this.getValue();

                    if (data.command != ECommand.Delete){
                        this.showMask();
                    }

                    if (loadFCK){
                        var fckAPI = FCKeditorAPI.GetInstance(message);
                        data.message = fckAPI.GetHTML();
                    }

                    OnSubmit.run(data, this);
               }
        });
        
        lbox = new Lbox();
        lbox.changeOptions({ 
                shadowThickness:{x:5,y:7},
                overlayOpacity:0.3,
                topPosition:150
        });

        showLB();

        if (options.command == ECommand.New){
            formsTest(true);
        }
    
        if (loadFCK){
            (function(){loadFCK();}).delay(1000,this);
        }

        function showLB(){
            buildLightBox();
        }
        function hideLB(){
            lbox.closeMilkbox();
        }
        function buildLightBox(){
            lbox.show(container);
        }

        function OnSubmit(data){
            WS.Process(
                data
                ,function(response){
                    if (data.command != ECommand.Delete){
                        this.hideMask();
                    }
                    call_back.run(response, this);
                }.bind(this)
                ,function(e){
                    if (data.command != ECommand.Delete){
                        this.hideMask();
                    }
                    call_back.run("<span>" + e + "</span><a href='" + data.return_url + "' >Click to return</a>", this);
                }.bind(this));
        }

        function call_back(response){
           container.set("html", (response));
        }
    },
    Cancel: function(){
       document.location.href = options.return_url;
    }
});